package com.example.egnotice1.controller;

import com.example.egnotice1.base.response.JsonResult;
import com.example.egnotice1.base.response.StatusCode;
import com.example.egnotice1.mapper.NoticeMapper;
import com.example.egnotice1.pojo.dto.NoticeAddParam;
import com.example.egnotice1.pojo.dto.NoticeQueryParam;
import com.example.egnotice1.pojo.dto.NoticeQueryParamv1;
import com.example.egnotice1.pojo.entity.Notice;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.example.egnotice1.base.response.JsonResult.success;
@Api(tags="资讯管理模块")//调整控制器的类名在knife4j的文档中显示的昵称
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @ApiOperation("添加资讯")
    @ApiOperationSupport(order=10)
    @PostMapping("add")
    @ApiImplicitParam(name = "noticeAddParam", value = "添加资讯参数", required = true, dataType = "NoticeAddParam")
    public JsonResult add(@RequestBody NoticeAddParam noticeAddParam) {
        //System.out.println("noticeAddParam"+noticeAddParam);
        log.debug("noticeAddParam"+noticeAddParam);
        Notice notice=new Notice();
        notice.setId(1L);
        notice.setUserId(2L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        BeanUtils.copyProperties(noticeAddParam,notice,"id","user_id","create_time","update_time" );
        int num=noticeMapper.insert(notice);
        log.debug("notice"+notice);
        if(num>0){
        return new JsonResult(StatusCode.OPERATION_SUCCESS);}
        else {return new JsonResult(StatusCode.OPERATION_FAIL);}
    }

    @ApiOperation("查询资讯")
    @ApiOperationSupport(order=20)
    @GetMapping("query")
    public JsonResult query(NoticeQueryParam noticeQueryParam) {
        log.debug("noticeQueryParam" + noticeQueryParam);
        String content = noticeQueryParam.getContent();
        Integer type = noticeQueryParam.getType();
        List<NoticeQueryParamv1> list =noticeMapper.NoticeAddQuery(content, type);
//        List<NoticeQueryParamv1> list =noticeMapper.query(noticeQueryParam);
        log.debug("------------"+list.size());
//        for(NoticeQueryParamv1 noticeQueryParamv1 : list) {
//            System.out.println(noticeQueryParamv1);
//        }
        log.debug(list.toString());
        if(type!=null)
        {
        return  success(list);}
        else{return success(null);}
    }

}
